<?php
namespace app\plus\notify;

use app\common\model\ShopOrderModel;
use app\common\model\ShopPromotionAuctionLogModel;
use app\common\model\ShopCardItemModel;
use app\common\model\ShopCardOrderModel;
use app\common\model\ShopPromotionGroupbuyCartModel;
use Payment\Notify\PayNotifyInterface;

/**
 * 异步通知
 * Class PayNotify
 * @package app\site\controller
 *
 */
class PayNotify implements PayNotifyInterface
{
    public function notifyProcess(array $data)
    {
        trace(json_encode($data), 'debug');
        $payCodeList = ['wx'=>'wxpay','ali'=>'alipay'];
        $payCode = $payCodeList[$data['channel']];
        switch ($data['extra_param']){
            case 'order': //普通订单支付
                $orderSn = $data['order_no'];
                $orderModel = new ShopOrderModel();
                $orderInfo = $orderModel->where('order_sn',$orderSn)->find();
                $orderModel->payOrder($orderInfo['buyer_username'],$orderInfo['order_sn'],$payCode,"[{$orderSn}]在线支付");
                return true;break;
            case 'auction'://集采保证金
                $payNo = $data['order_no'];
                $model=new ShopPromotionAuctionLogModel();
                if ($model->where('pay_sn',$payNo)->setField('status',1)){
                    return true;
                }else{
                    return false;
                }break;
            case 'groupbuy_cart'://竞拍保证金
                $orderNo = $data['order_no'];
                $model   = new ShopPromotionGroupbuyCartModel();
                $back    = $model->where('order_sn', $orderNo)->update(['margin_status'=>1,'margin_time'=>time()]);
                if ($back) {
                    return true;
                } else {
                    return false;
                }break;
            case 'card'://购买优惠券
                $orderNo = $data['order_no'];
                $orderModel   = new ShopCardOrderModel();
                $itemModel=new ShopCardItemModel();
                $orderInfo=$orderModel->where('order_sn', $orderNo)->find();
                $orderModel->where('order_sn', $orderNo)->setField('status', 1);
                $back    = $itemModel->where('item_id', $orderInfo['item_id'])->setField('status', 1);
                if ($back) {
                    return true;
                } else {
                    return false;
                }break;
        }
    }
}
